/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue';
import Router from 'vue-router';
// import http from '@/utils/httpRequest'

import {clearLoginInfo} from '@/utils';

Vue.use(Router);

// 全局路由(无需嵌套上左右整体布局)

const globalRoutes = [
    // {
    //   path: '/404',
    //   component: () => import('@/components/404'),
    //   name: '404',
    //   meta: {
    //     title: '404未找到', // eslint-disable-line
    //   }, // eslint-disable-line
    // },
    {
        path: '/login',
        component: () => import('@/components/login'),
        name: 'login',
        meta: {
            title: '登录',
        },
    },
    {
        path: '/',
        component: () => import('@/views/main'),
        name: 'main',
        redirect: '/login',
        meta: {
            title: '登录',
        },
        children: [
            {
                path: '/unitMap',
                component: () => import('@/views/modules/preview/unitMap'),
                name: 'unitMap',
            },
            {
                path: '/equipmentMap',
                component: () => import('@/views/modules/preview/equipmentMap'),
                name: 'equipmentMap',
            },
            {
                path: '/monitoring',
                component: () => import('@/views/modules/monitoring'),
                name: 'monitoring',
            },
            {
                path: '/monitoring/imgMessage',
                component: () => import('@/views/modules/monitoring/imgMessage'),
                name: 'imgMessage',
            },
            {
                path: '/monitoring/imgShow',
                component: () => import('@/views/modules/monitoring/imgShow'),
                name: 'imgShow',
            },
            {
                path: '/monitoring/placeMessage',
                component: () => import('@/views/modules/monitoring/placeMessage'),
                name: 'placeMessage',
            },
            {
                path: '/userinfo',
                component: () => import('@/views/modules/userinfo/subaccount'),
                name: 'userinfo',
            },
            {
                path: '/userinfo/information',
                component: () => import('@/views/modules/userinfo/information'),
                name: 'information',
            },
            {
                path: '/userinfo/alterCode',
                component: () => import('@/views/modules/userinfo/alterCode'),
                name: 'alterCode',
            },
            {
                path: '/unit',
                component: () => import('@/views/modules/unit'),
                name: 'unit',
                meta: {
                    title: '首页',
                },
            },
            {
                path: '/equipment',
                component: () => import('@/views/modules/equipment'),
                name: 'equipment',
                meta: {
                    title: '消息中心',
                    isTab: true,
                },
            },
        ],
        beforeEnter(to, from, next) {
            // Vue.cookie.delete('token')
            let token = Vue.cookie.get('token');
            if (!token || !/\S/.test(token)) {
                clearLoginInfo();
                next({
                    name: 'login',
                });
            }
            next();
        },
    },
];

const router = new Router({
    mode: 'hash',
    scrollBehavior: () => ({
        y: 0,
    }),
    // routes: globalRoutes.concat(mainRoutes),
    routes: globalRoutes,
});

router.beforeEach((to, from, next) => {
    next();
});
// 获取原型对象push函数
const originalPush = Router.prototype.push;

// 获取原型对象replace函数
const originalReplace = Router.prototype.replace;

// 修改原型对象中的push函数
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err);
};

// 修改原型对象中的replace函数
Router.prototype.replace = function replace(location) {
    return originalReplace.call(this, location).catch(err => err);
};
export default router;
